package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Validated
@Tag(name = "套餐管理")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     * @param
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<PageResult<SetmealVO>> page(@Valid SetmealPageQueryDTO setmealPageQueryDTO) {
       PageResult<SetmealVO> pageSetmeal = setmealService.pageSetmeal(setmealPageQueryDTO);
       if (ObjectUtils.isNotEmpty(pageSetmeal)){
           return Result.success(pageSetmeal);
       }else {
           return Result.error(MessageConstant.NOT_FOUND_QUERY);
       }
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @Operation(summary = "新增套餐")
    @CacheEvict(cacheNames = "sky-take-out:setmealCache",key = "#setmealDTO.categoryId")//key:sky-take-out:setmealCache::100
    public Result<String> save(@Valid @RequestBody SetmealDTO setmealDTO) {
        setmealService.saveSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询套餐")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        if (ObjectUtils.isNotEmpty(setmealVO)){
            return Result.success(setmealVO);
        }else {
            return Result.error(MessageConstant.NOT_FOUND_QUERY);
        }
    }
    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @Operation(summary = "修改套餐")
    @CacheEvict(cacheNames = "sky-take-out:setmealCache",allEntries = true)
    public Result<String> update(@Valid @RequestBody SetmealDTO setmealDTO) {
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @Operation(summary = "套餐起售、停售")
    @CacheEvict(cacheNames = "sky-take-out:setmealCache",allEntries = true)
    public Result<String> setmealStartOrStopStatus(@PathVariable Integer status, Long id) {
        setmealService.startOrStopStatus(status, id);
        return Result.success();
    }
    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @Operation(summary = "批量删除套餐")
    @CacheEvict(cacheNames = "sky-take-out:setmealCache",allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids) {
        setmealService.deleteBatchByIds(ids);
        return Result.success();
    }

}
